#include "FoGGEDEF.h"
#include ".\exceptionhandler.h"
#include "Logger.h"
#include <stdio.h>
#include <stdlib.h>
#include "BugSlayer/Include/BugslayerUtil.h"
//link lib
#ifdef _DEBUG
	#pragma comment(lib,"BugslayerUtil_d.lib")
#else
	#pragma comment(lib,"BugslayerUtil.lib")
			
#endif



namespace _FOGGE_
{
	namespace _UTIL_
	{

		
		ExceptionHandler::ExceptionHandler(void)
		{
			m_bGetFaultReason=true;
			m_bGetStackTraceString=true;
			m_bShowRegs=false;
			m_bLetItCrash=false;
			m_dwOpts=GSTSO_PARAMS | GSTSO_MODULE | GSTSO_SYMBOL | GSTSO_SRCLINE;

		}

		ExceptionHandler::~ExceptionHandler(void)
		{

		}

		void ExceptionHandler::ShowFaultReason(bool flag)
		{
			m_bGetFaultReason=flag;
		}
		void ExceptionHandler::EnableStackTrace(bool flag)
		{
			m_bGetStackTraceString=flag;
		}
		void ExceptionHandler::ShowRegister(bool flag)
		{
			m_bShowRegs=flag;
		}
		void ExceptionHandler::SetOption(DWORD option)
		{
			m_dwOpts=option;
		}
		void ExceptionHandler::SetLetItCrash(bool flag)
		{
			m_bLetItCrash=flag;
		}

		LONG ExceptionHandler::ExcepCallBack ( EXCEPTION_POINTERS * pExPtrs )
		{
			if ( true == m_bGetFaultReason )
			{
				logWriteLine_Broadcast(Logger::eLL_Exception, GetFaultReason ( pExPtrs ));
				
			}

			if ( true == m_bShowRegs )
			{
				logWriteLine_Broadcast(Logger::eLL_Exception, GetRegisterString ( pExPtrs ));
				
			}

			if ( TRUE == m_bGetStackTraceString )
			{
				LPCTSTR  szBuff = GetFirstStackTraceString ( GSTSO_PARAMS | GSTSO_MODULE | GSTSO_SYMBOL | GSTSO_SRCLINE  ,	pExPtrs  ) ;
				do
				{
					logWriteLine_Broadcast(Logger::eLL_Exception, szBuff );
					szBuff = GetNextStackTraceString ( GSTSO_PARAMS | GSTSO_MODULE | GSTSO_SYMBOL | GSTSO_SRCLINE , pExPtrs ) ;
				}
				while ( NULL != szBuff ) ;
			}

			if ( m_bLetItCrash )
			{
				return ( EXCEPTION_CONTINUE_SEARCH ) ;
			}
			else
			{
				return ( EXCEPTION_EXECUTE_HANDLER ) ;
			}
		}
	}
}

